import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class GoodsCase extends StatefulWidget {
  const GoodsCase({super.key});
  @override
  State createState() => _GoodsCase();
}

class _GoodsCase extends State<GoodsCase> {
  List<OrderItem> orderInfo = [];
  @override
  void initState() {
    loadData();
    super.initState();
  }

  loadData() async {
    Dio dio = Dio();
    Response res = await dio.get('https://mock.boxuegu.com/mock/1172/orders');
    orderInfo = [];
    for (var item in (res.data as List)) {
      orderInfo.add(OrderItem(
          attrsText: item['attrsText'],
          createTime: item['createTime'],
          curPrice: item['curPrice'],
          totalMoney: item['totalMoney'],
          totalNum: item['totalNum'],
          image: item['image'],
          orderState: item['orderState'],
          name: item['name']));
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('订单列表页'),
        ),
        body: Container(
          color: Colors.white,
          child: ListView.builder(
            itemBuilder: (context, int index) {
              return GoodItem(orderItem: orderInfo[index]);
            },
            itemCount: orderInfo.length,
          ),
        ));
  }
}

// ignore: must_be_immutable
class GoodItem extends StatefulWidget {
  GoodItem({super.key, required this.orderItem});
  OrderItem orderItem;
  @override
  State createState() => _GoodItem();
}

class _GoodItem extends State<GoodItem> {
  /// 生成订单状态的方法
  String getOrderState(int state) {
    String orderState = '待付款';
    switch (state) {
      case 1:
        orderState = '待付款';
        break;
      case 2:
        orderState = '待发货';
        break;
      case 3:
        orderState = '待收货';
        break;
      case 4:
        orderState = '待评价';
        break;
      default:
        orderState = '其他';
    }

    return orderState;
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(widget.orderItem.createTime),
              Text(getOrderState(widget.orderItem.orderState))
            ],
          ),
          Padding(
            padding: const EdgeInsets.only(top: 10),
            child: Row(
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(10),
                  child: Image.network(widget.orderItem.image,
                      width: 100, height: 100),
                ),
                Expanded(
                    child: Padding(
                  padding: const EdgeInsets.only(left: 10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Expanded(child: Text(widget.orderItem.name)),
                          Padding(
                            padding: const EdgeInsets.only(left: 10),
                            child: Text('x ${widget.orderItem.totalNum}'),
                          ),
                        ],
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 5),
                        child: Text(widget.orderItem.attrsText,
                            style: const TextStyle(
                              color: Color(0xFF888888),
                            )),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 5),
                        child: Text('￥${widget.orderItem.curPrice}'),
                      ),
                    ],
                  ),
                )),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [Text('合计：￥${widget.orderItem.totalMoney}')],
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 10),
            child: Container(
              alignment: Alignment.centerRight,
              child: Container(
                padding: const EdgeInsetsDirectional.symmetric(
                    vertical: 5, horizontal: 10),
                decoration:
                    BoxDecoration(border: Border.all(color: Colors.black)),
                child: const Text('再次购买'),
              ),
            ),
          )
        ],
      ),
    );
  }
}

class OrderItem {
  String createTime = '';
  int orderState = 0;
  String image = '';
  String name = '';
  int totalNum = 0;
  double curPrice = 0;
  double totalMoney = 0;
  String attrsText = '';
  OrderItem(
      {required this.createTime,
      required this.orderState,
      required this.image,
      required this.name,
      required this.totalNum,
      required this.curPrice,
      required this.totalMoney,
      required this.attrsText});
}
